package com.resolution.dietaryassessment.view.chronic.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.listener.MyListener;
import com.resolution.dietaryassessment.view.chronic.model.Question;

/**
 * @author By Resolusion
 * @Title:
 * @Date: 2020/2/23
 * @Time: 22:22
 * @email Res01ution@foxmail.com
 */
public class ChronicOneFrag extends Fragment implements RadioGroup.OnCheckedChangeListener {


    private RadioGroup mRadioGroup;
    private RadioButton rbChooseOne;
    private RadioButton rbChooseTwo;
    private RadioButton rbChooseThree;
    private RadioButton rbChooseFour;

    private TextView title;

    private View view;
    private MyListener myListener;
    private Question question;

    public static ChronicOneFrag getInstance(Question question){
        ChronicOneFrag frag = new ChronicOneFrag();
        Bundle bundle = new Bundle();
        bundle.putSerializable("question",question);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myListener = MyListener.getInstance(getContext());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.fragment_chronic_one,container,false);
        myListener.setFLAG(MyListener.NO_CHOICE);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        question = (Question) bundle.getSerializable("question");

        title.setText(question.getTitle());
        rbChooseOne.setText(question.getFirst());
        rbChooseTwo.setText(question.getSecond());
        rbChooseThree.setText(question.getThird());
        rbChooseFour.setText(question.getFourth());
        if (question.getFourth().equals("")){
            rbChooseFour.setVisibility(View.GONE);
        }
    }

    private void initView(){
        //radiobutton
        rbChooseOne = view.findViewById(R.id.rb_choose_one);
        rbChooseTwo = view.findViewById(R.id.rb_choose_two);
        rbChooseThree = view.findViewById(R.id.rb_choose_three);
        rbChooseFour = view.findViewById(R.id.rb_choose_four);
        //textView
        title = view.findViewById(R.id.title);

        Button nextSubject = view.findViewById(R.id.next_subject);
        mRadioGroup = view.findViewById(R.id.group_radio);

        nextSubject.setOnClickListener(myListener);
        mRadioGroup.setOnCheckedChangeListener(this);
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String userAnswer = "";
        switch (checkedId) {
            case R.id.rb_choose_one:
                userAnswer = rbChooseOne.getText().toString();
                break;
            case R.id.rb_choose_two:
                userAnswer = rbChooseTwo.getText().toString();
                break;
            case R.id.rb_choose_three:
                userAnswer = rbChooseThree.getText().toString();
                break;
            case R.id.rb_choose_four:
                userAnswer = rbChooseFour.getText().toString();
                break;
        }
        if (question.getAnswer().equals(userAnswer)) {
            myListener.setFLAG(MyListener.TRUE_CHOICE);
        } else {
            myListener.setFLAG(MyListener.FALSE_CHOICE);
        }
    }
}
